//
//  ChannelsViewController.m
//  doubanFun
//
//  Created by bing on 16/3/24.
//  Copyright © 2016年 bing. All rights reserved.
//

#import "AppDelegate.h"
#import "DFChannel.h"
#import "RESTClient.h"
#import "ChannelCell.h"
#import "ChannelsViewController.h"

@interface ChannelsViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *sortChannels;

@end

@implementation ChannelsViewController

- (void)setup {
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = [UIColor whiteColor];
    UINib *nib = [UINib nibWithNibName:@"ChannelCell" bundle:[NSBundle mainBundle]];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshView)
                                                 name:CHANNEL_LOAD_SUCCESS object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
    [self refreshView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.f;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sortChannels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChannelCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    DFChannel *channel = [self.sortChannels objectAtIndex:indexPath.row];
    cell.name.text     = channel.name;
    
    NSString *channelID = _AppDelegate.config.currentChannelID;
    if ([channelID isEqualToString:channel.channel_id] || channelID.length < 1)
        cell.audio.image = [UIImage imageNamed:@"audio"];
    else
        cell.audio.image = [UIImage new];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *headerView = [[UILabel alloc] init];
    
    return headerView;
}


#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(viewController:didChannel:index:)]) {
        DFChannel *channel = [self.sortChannels objectAtIndex:indexPath.row];
        [self.delegate viewController:self didChannel:channel index:indexPath];
        [self refreshView];
    }
}


#pragma mark - refreshView

- (void)refreshView {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}


#pragma mark - lazy

- (NSArray *)sortChannels {
    if (!_sortChannels) {
        // TODO:排序规则修改
        _sortChannels = [[ChannelPool sharedPool] allChannelsAndSorted];
        // 艺术家兆赫
        // 单曲兆赫
        // 语言年代兆赫
        // 风格流派兆赫
        // 心情场景兆赫
        // 品牌兆赫
        
        for (DFChannel *c in _sortChannels) {
            NSLog(@"channal:%@---seq_id:%ld---id:%@",c.name,(long)c.seq_id,c.channel_id);
        }
    }
    return _sortChannels;
}

@end
